/*
 * Copyright (c) 2023-2025 110476592@qq.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * @file abs_float_toolbar.cpp
 * @brief 截图工具 
 * @author 110476592@qq.com 
 * @note   承接Qt项目外包定制开发。长期接收C++项目实训学徒,联系QQ：110476592。
 */

 
#include <common/global.h>
#include "abs_float_toolbar.h"
#include <QToolButton>
#include <QAction>

#include "toolbuttonex.h"

AbsFloatToolBar::AbsFloatToolBar(QWidget *parent)
    : QWidget{parent}
{
    setObjectName("AbsFloatToolBar");
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    // 设置窗口属性
    setProperty("widgetType", "FloatToolBar");
    // 禁止父窗口样式影响子控件样式
    setAttribute(Qt::WA_StyledBackground);
    // 去除边框
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint |
                   Qt::WindowMinMaxButtonsHint| Qt::WindowStaysOnTopHint);


    setFixedHeight(36);
    m_hLayout = new QHBoxLayout(this);
    m_vLayout = new QVBoxLayout(this);
    m_hLayout->setContentsMargins(10, 0, 10, 0);
    m_hLayout->setSpacing(8);

    m_vLayout->setContentsMargins(10, 0, 10, 0);
    m_vLayout->setSpacing(8);
    m_buttonGroup = new QButtonGroup(this);
}

AbsFloatToolBar::~AbsFloatToolBar()
{

}

void AbsFloatToolBar::enableButton(CommandType cmdId,bool enable)
{
    for(ToolButtonEx *btn :m_buttons)
    {
        if(btn->commandType() == cmdId){
            btn->setEnabled(enable);
            return ;
        }
    }
}

ToolButtonEx* AbsFloatToolBar::addToolButton(const QString & title, int fontCode  , CommandType cmdType){

    ToolButtonEx * btn = new ToolButtonEx(cmdType,this);
    btn->setFontId(fontCode);

    btn->setToolButtonStyle(Qt::ToolButtonIconOnly);
    btn->setFixedHeight(24);
    btn->setToolTip(title);

    bool ok = true;
    ushort num = QString::number(fontCode, 16).toUShort(&ok, 16);
    el::ELIconUtil::setIcon(btn, QChar(num),"#fff",18);

    connect(btn, &ToolButtonEx::clicked, this,[=](){
        emit this->onClicked(btn);
    });
    m_buttons.push_back(btn);
    return btn;
}



QSize AbsFloatToolBar::preferredSize()
{
    QSize palettePreferredSize = sizeHint();

    if (palettePreferredSize.width() == 0)
    {
        palettePreferredSize = childrenRect().size();
    }

    return palettePreferredSize;
}
